<script setup lang="ts">
import { ref, watch, computed } from "vue";

interface Props {
  standard: any;
  experience: any;
  selectedVersion: "standard" | "experience";
  startServiceTimes?: number;
}

const props = defineProps<Props>();
const emit = defineEmits(["update:selectedVersion", "closeDialog"]);

const localSelectedVersion = ref(props.selectedVersion);

watch(
  () => props.selectedVersion,
  (newValue) => {
    localSelectedVersion.value = newValue;
  }
);

const closeDialog = (params = {}) => {
  emit("closeDialog", params);
};

const changeVersion = (version: "standard" | "experience") => {
  localSelectedVersion.value = version;
  emit("update:selectedVersion", version);
};

const saveSelection = () => {
  emit("update:selectedVersion", localSelectedVersion.value);
  closeDialog();
};

const getSelectedVersionDetails = computed(() => {
  return localSelectedVersion.value === "standard"
    ? props.standard
    : props.experience;
});

const formatPrice = (price: number | undefined) => {
  if (price === undefined) return "N/A";
  return new Intl.NumberFormat("zh-CN", {
    style: "currency",
    currency: "CNY",
  }).format(price);
};

const serviceTimeText = computed(() => {
  const timeType = props.startServiceTimes;
  switch (timeType) {
    case 0:
      return "24小时内";
    case 1:
      return "3天内";
    case 2:
      return "7天内";
    case 3:
      return "任何时候";
  }
  return "立刻";
});
</script>

<template>
  <div class="dialog-contact" @click="closeDialog">
    <div class="dialog-content" @click.stop>
      <div class="dialog-header">
        <div class="left">
          <img src="../../assets/dialog-icon.png" alt="" />
          <span>选择服务规格</span>
        </div>
        <img
          class="close"
          src="../../assets/modal-close.png"
          alt=""
          @click="closeDialog"
        />
      </div>
      <div class="dialog-body">
        <div class="class">
          <p class="tab">
            <span
              :class="{ active: localSelectedVersion === 'standard' }"
              @click="changeVersion('standard')"
            >
              标准版
            </span>
            <span
              :class="{ active: localSelectedVersion === 'experience' }"
              @click="changeVersion('experience')"
            >
              体验版
            </span>
          </p>
          <p class="price" v-if="getSelectedVersionDetails">
            <span>¥</span
            >{{ formatPrice(getSelectedVersionDetails.goodsAmount).slice(1) }}
          </p>
          <p class="service-content" v-if="getSelectedVersionDetails">
            {{ getSelectedVersionDetails.introduce }}
          </p>
          <p class="jf" v-if="getSelectedVersionDetails">
            <span>交付周期</span
            ><span>{{ getSelectedVersionDetails.deliverDays }}天</span>
          </p>
          <p class="jf"><span>开启服务时间</span><span>{{ serviceTimeText }}</span></p>
          <div
            v-if="
              getSelectedVersionDetails &&
              getSelectedVersionDetails.detailsServer
            "
            v-for="item in getSelectedVersionDetails.detailsServer"
            :key="item.id"
            class="item"
          >
            <div class="item-title">
              <span class="r"></span>
              <span class="text">{{ item.content }}</span>
            </div>
            <img src="../../assets/goods-select.png" alt="" />
          </div>
        </div>
      </div>
      <div class="dialog-footer">
        <span @click="closeDialog">取消</span>
        <span @click="saveSelection">保存</span>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.class {
  .tab {
    line-height: 36px;
    border-bottom: 1px solid #ededee;
    span {
      &:nth-child(1) {
        margin-right: 24px;
      }
      &.active {
        color: #019a81;
        position: relative;
        &:after {
          content: "";
          position: absolute;
          bottom: -10px;
          left: 50%;
          width: 42px;
          height: 2px;
          background: #019a81;
          margin-left: -21px;
        }
      }
    }
  }
  .price {
    font-size: 30px;
    font-weight: bold;
    padding-top: 20px;
    padding-bottom: 8px;
    color: #ff4f4f;
    span {
      font-size: 20px;
    }
  }
  .service-content {
    color: rgba(25, 30, 40, 0.6);
    margin-bottom: 20px;
  }
  .jf {
    display: flex;
    align-items: center;
    line-height: 24px;
    margin-bottom: 16px;
    font-weight: bold;
    span:nth-child(1) {
      flex: 1;
    }
    span:nth-child(2) {
      color: #019a81;
    }
  }
  .item {
    display: flex;
    align-items: center;
    margin-bottom: 8px;
    line-height: 24px;
    .item-title {
      flex: 1;
      display: flex;
      align-items: center;
      .r {
        width: 4px;
        height: 4px;
        background: rgba(25, 30, 40, 0.1);
        margin-right: 4px;
      }
      .text {
        color: #191e28;
      }
    }
    img {
      width: 20px;
      height: 20px;
    }
  }
}
.dialog-contact {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(25, 30, 40, 0.5);
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
}
.dialog-content {
  width: 430px;
  background: #fff;
  border-radius: 12px 12px 12px 12px;
  padding: 24px;
  box-sizing: border-box;
}
.dialog-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  .left {
    flex: 1;
    display: flex;
    align-items: center;
    img {
      width: 20px;
      height: 20px;
      margin-right: 4px;
    }
    span {
      font-size: 16px;
      font-weight: bold;
    }
  }
  .close {
    cursor: pointer;
    width: 16px;
    height: 16px;
  }
}
.dialog-footer {
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  margin-top: 36px;
  span {
    width: 92px;
    height: 36px;
    text-align: center;
    line-height: 36px;
    cursor: pointer;
    &:nth-child(1) {
      color: rgba(25, 30, 40, 0.6);
      border-radius: 8px 8px 8px 8px;
      border: 1px solid rgba(25, 30, 40, 0.08);
    }
    &:nth-child(2) {
      background: #1dbf73;
      border-radius: 8px 8px 8px 8px;
      border: 1px solid rgba(25, 30, 40, 0.08);
      color: #fff;
      margin-left: 12px;
    }
  }
}
</style>
